Comments On Code

Posts on Automator

Sep 4, 2013

Predict Fertility in iCal

calendarsAnyone that knows us in real life, knows we're committed Catholics with a large family. We follow Church teaching and avoid contraception and artificial birth control. Having tried several methods of NFP, I have come to the conclusion that at this point in time, a binary system works best for us.

We've tried a few iOS apps, but find their algorithms suspect at best.  CycleBeads are simple and very straightforward. For women whose cycles fall in the range of 26-32 days, the fertile period should fall somewhere in the days 8-19 range. If you're open to adding to your family, use those days. If you're not able at this time, don't. The system has been widely tested and available for many years. It is used effectively in the third world, and claims at least 95% effectiveness.

My history falls well within the requirements for accuracy in the CycleBeads system. But with a large family, a budding new business and in school myself full-time, I need something accurate and obvious. I can't trust myself to move a marker daily, and don't have time to sit and recount nightly.

So, I've created a way to make it even more dead simple using AppleScript, Automator and Calendar (iCal).  On cycle Day 1, I can run an app that automatically enters events for days 8-19 into Calendar, with alerts which tell me which day I'm currently on at 8:00am every morning.

Here's how:

First, create a calendar to hold the cycle data.  Use Command + Space bar to open Spotlight and start typing 'Calendar'.  Hit enter when the Calendar App appears highlighted.  Open the File drop down from the Calendar menu and click "New Calendar".   Name the Calendar 'Fertile'.  Make sure the Calendar sidebar is open on the left of the window.  If it isn't, click the 'Calendars' button at the top left corner of the window.  Click the new calendar you created and hit Command + I to open the Get Info dialog.  Click the color box to choose a suitable color for your events in this calendar.  I chose a spring green for new life/growing.  Click 'OK'.

Now that we have the calendar set up, we'll shut off the default settings for alerts so that we can designate only the alerts that we set.  Open the Calendar drop down from the Application menu and click 'Preferences'.  Click the Alerts tab and set the All Day Events default to 'None'.  Note: if you have created other events with the default setting on, you will need to go in and manually set desired alerts on each event.  Alternatively, you can also choose one of the other options in the default preferences and skip adding alerts in the next step.

Now for the fun stuff.  Open Applescript (Command + Space and start typing 'Applescript', hitting enter when the App name is highlighted).  Type or copy and paste the following script:

tell application "Calendar"

tell calendar "Fertile"

set theDate to current date

set newEvent to make new event at end with properties {summary:"Day 8:Fertile", start date:theDate + 8 * days, allday event:true}

make new display alarm at end of display alarms of newEvent with properties {trigger interval:480}

set newEvent to make new event at end with properties {summary:"Day 9:Fertile", start date:theDate + 9 * days, allday event:true}

make new display alarm at end of display alarms of newEvent with properties {trigger interval:480}

set newEvent to make new event at end with properties {summary:"Day 10:Fertile", start date:theDate + 10 * days, allday event:true}

make new display alarm at end of display alarms of newEvent with properties {trigger interval:480}

set newEvent to make new event at end with properties {summary:"Day 11:Fertile", start date:theDate + 11 * days, allday event:true}

make new display alarm at end of display alarms of newEvent with properties {trigger interval:480}

set newEvent to make new event at end with properties {summary:"Day 12:Fertile", start date:theDate + 12 * days, allday event:true}

make new display alarm at end of display alarms of newEvent with properties {trigger interval:480}

set newEvent to make new event at end with properties {summary:"Day 13:Fertile", start date:theDate + 13 * days, allday event:true}

make new display alarm at end of display alarms of newEvent with properties {trigger interval:480}

set newEvent to make new event at end with properties {summary:"Day 14:Fertile", start date:theDate + 14 * days, allday event:true}

make new display alarm at end of display alarms of newEvent with properties {trigger interval:480}

set newEvent to make new event at end with properties {summary:"Day 15:Fertile", start date:theDate + 15 * days, allday event:true}

make new display alarm at end of display alarms of newEvent with properties {trigger interval:480}

set newEvent to make new event at end with properties {summary:"Day 16:Fertile", start date:theDate + 16 * days, allday event:true}

make new display alarm at end of display alarms of newEvent with properties {trigger interval:480}

set newEvent to make new event at end with properties {summary:"Day 17:Fertile", start date:theDate + 17 * days, allday event:true}

make new display alarm at end of display alarms of newEvent with properties {trigger interval:480}

set newEvent to make new event at end with properties {summary:"Day 18:Fertile", start date:theDate + 18 * days, allday event:true}

make new display alarm at end of display alarms of newEvent with properties {trigger interval:480}

set newEvent to make new event at end with properties {summary:"Day 19:Fertile", start date:theDate + 19 * days, allday event:true}

make new display alarm at end of display alarms of newEvent with properties {trigger interval:480}

end tell

end tell

Save it as 'Cycle'.  Since AppleScript for Calendar reads mostly like English, it is fairly straightforward what the script is doing.  Note that if you named your calendar something other than 'Fertile' you need to adjust the code to suit.  Also, if you have chosen the default alerts, you can remove each line that begins "make new display alarm".  What if you want your alert to appear at a time other than 8AM?  Simply set the time in minutes after 'trigger interval': 60 minutes in an hour, 8 * 60 = 480, therefore, 8AM.  Also, you may use negative numbers to set the alert to display for a designated number of minutes before the event.

As a final step, we'll make this script into an app.  Use spotlight (Command + Space bar) to open Automator.  Choose Application as the file type.  Scroll down the Actions menu until you find the 'Run AppleScript' action.  Double click or drag and drop into the box on the right.  Highlight the line in the parentheses and paste in the script.  If you made changes to the script I included, make sure to copy and paste your own version.  Name it 'Cycle' and save in your Applications folder.

Now, when you run the app, it will create all-day events with reminders between 8 and 19 days from the current date.

Resources: